package me.zk.practice1.hw;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangkai
 * @date 2023/8/8 15:36
 */
public class HWBJ07 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String s1 = scanner.nextLine();

        String s2 = scanner.nextLine();

        Map<String,Integer> l1 = getMap(s1);

        Map<String,Integer> l2 = getMap(s2);
        List<Student> list = new ArrayList<>();
        for (String s : l1.keySet()) {
            if (l2.containsKey(s)) {
                list.add(new Student(l1.get(s)+l1.get(s),s));
            }
        }
        Map<String,List<Student>> m2 = list.stream().collect(Collectors.groupingBy(s->s.id.substring(0,5)));

         m2.keySet().stream().sorted().forEach(key ->{
                List<Student> stus = m2.get(key);
                Collections.sort(stus);
                System.out.println(key);
                StringBuffer stringBuffer = new StringBuffer();
             for (int i = 0; i < stus.size(); i++) {
                 stringBuffer.append(stus.get(i).id);
                if (i != stus.size()-1){
                    stringBuffer.append(";");
                }
             }
             System.out.println(stringBuffer);
         });


    }

    public static Map<String,Integer> getMap(String s) {

        Map<String,Integer> map = new HashMap<>();
        String[] str = s.split(";");
        for (String value : str) {
            String[] s1 = value.split(",");
            map.put(s1[0],Integer.parseInt(s1[1]));
        }
        return map;
    }

    static class Student implements Comparable<Student> {
        int total;
        String id;

        public Student(int total, String id) {
            this.total = total;
            this.id = id;
        }

        @Override
        public int compareTo(Student s) {
            if (s.total == this.total) {
                return this.id.compareTo(s.id);
            }
            return s.total-this.total;
        }
    }
}
